package cn.wapache.time.trigger;

import java.util.LinkedList;
import java.util.List;

import cn.wapache.time.calendar.Calendar;

/**
 * Convenience and utility methods for working with <code>{@link Trigger}s</code>.
 * 
 * 
 * @see CronTrigger
 * @see SimpleTrigger
 * @see DateBuilder
 * 
 * @author James House
 */
public class TriggerUtils {

    /**
     * Private constructor because this is a pure utility class.
     */
    private TriggerUtils() {
    }
    
    /*
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * 
     * Interface.
     * 
     * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     */

    /**
     * Returns a list of Dates that are the next fire times of a <code>Trigger</code>.
     * The input trigger will be cloned before any work is done, 
     * so you need not worry about its state being altered by this method.
     * 
     * @param trigg
     *          The trigger upon which to do the work
     * @param cal
     *          The calendar to apply to the trigger's schedule
     * @param numTimes
     *          The number of next fire times to produce
     * @return List of java.util.Date objects
     */
    public static List<Long> computeFireTimes(Trigger trigg, Calendar cal, int numTimes) {
        LinkedList<Long> lst = new LinkedList<>();

        Trigger t = (Trigger) trigg.clone();

        if (t.getNextFireTime() <= 0) {
            t.computeFirstFireTime(cal);
        }

        for (int i = 0; i < numTimes; i++) {
            long d = t.getNextFireTime();
            if (d >0) {
                lst.add(d);
                t.triggered(cal);
            } else {
                break;
            }
        }

        return java.util.Collections.unmodifiableList(lst);
    }
    
    /**
     * Compute the <code>Date</code> that is 1 second after the Nth firing of the given <code>Trigger</code>, 
     * taking the triger's associated <code>Calendar</code> into consideration.
     *  
     * The input trigger will be cloned before any work is done, 
     * so you need not worry about its state being altered by this method.
     * 
     * @param trigg
     *          The trigger upon which to do the work
     * @param cal
     *          The calendar to apply to the trigger's schedule
     * @param numTimes
     *          The number of next fire times to produce
     * @return the computed Date, or null if the trigger (as configured) will not fire that many times.
     */
    public static long computeEndTimeToAllowParticularNumberOfFirings(Trigger trigg, Calendar cal, int numTimes) {

    	Trigger t = (Trigger) trigg.clone();

        if (t.getNextFireTime() <= 0) {
            t.computeFirstFireTime(cal);
        }
        
        int c = 0;
        long endTime = 0;
        
        for (int i = 0; i < numTimes; i++) {
            long d = t.getNextFireTime();
            if (d > 0) {
                c++;
                t.triggered(cal);
                if(c == numTimes)
                    endTime = d;
            } else {
                break;
            }
        }
        
        if(endTime <= 0)
            return 0;
        
        endTime = endTime + 1000L;
        
        return endTime;
    }

    /**
     * Returns a list of Dates that are the next fire times of a 
     * <code>Trigger</code>
     * that fall within the given date range. The input trigger will be cloned
     * before any work is done, so you need not worry about its state being
     * altered by this method.
     * 
     * <p>
     * NOTE: if this is a trigger that has previously fired within the given
     * date range, then firings which have already occurred will not be listed
     * in the output List.
     * </p>
     * 
     * @param trigg
     *          The trigger upon which to do the work
     * @param cal
     *          The calendar to apply to the trigger's schedule
     * @param from
     *          The starting date at which to find fire times
     * @param to
     *          The ending date at which to stop finding fire times
     * @return List of java.util.Date objects
     */
    public static List<Long> computeFireTimesBetween(Trigger trigg, Calendar cal, long from, long to) {
        LinkedList<Long> lst = new LinkedList<>();

        Trigger t = (Trigger) trigg.clone();

        if (t.getNextFireTime() <= 0) {
            t.setStartTime(from);
            t.setEndTime(to);
            t.computeFirstFireTime(cal);
        }

        while (true) {
            long d = t.getNextFireTime();
            if (d > 0) {
                if (d < from) {
                    t.triggered(cal);
                    continue;
                }
                if (d > to) {
                    break;
                }
                lst.add(d);
                t.triggered(cal);
            } else {
                break;
            }
        }

        return java.util.Collections.unmodifiableList(lst);
    }

}
